package com.splant.android.smart.ModelLevel;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.splant.android.R;
import com.splant.android.smart.SystemDefinedLevel.Utils.System_Dimen_Util;
import com.splant.android.smart.UILevel.Community.Garden_Water_Activity;
import com.splant.android.smart.UILevel.Utils.sPlantUitl;

/**
 * Created by Administrator on 2017/8/23.
 */

public class WaterMapObject {

    private View waterView;
    private int deviceType;
    private float leftScale;
    private float topScale;
    private TextView tv_switch0 = null;
    private TextView tv_switch1 = null;
    private TextView tv_switch2 = null;
    private ImageView img_water = null;

    private TextView tv_waterName;
    private WaterListBean.Water water;
    private Activity activity;

    public WaterMapObject(final Activity activity, final WaterListBean.Water water) {

        this.activity=activity;
        this.water = water;

        deviceType = sPlantUitl.getWaterType(water.getWaterId());

        if (deviceType == 1) {

            waterView = activity.getLayoutInflater().inflate(R.layout.garden_map_water_1out, null);
            img_water = (ImageView) waterView.findViewById(R.id.img_water);

            if (water.getWaterState() == 1)
                img_water.setImageResource(R.mipmap.garden_ic_water1_online);
            else
                img_water.setImageResource(R.mipmap.garden_ic_water1_offline);

        } else if (deviceType == 2) {
            waterView = activity.getLayoutInflater().inflate(R.layout.garden_map_water_2out, null);
            tv_switch1 = (TextView) waterView.findViewById(R.id.tv_switch1);
            img_water = (ImageView) waterView.findViewById(R.id.img_water);

            if (water.getWaterState() == 1)
                img_water.setImageResource(R.mipmap.garden_ic_water2_online);
            else
                img_water.setImageResource(R.mipmap.garden_ic_water2_offline);

        } else {

            waterView = activity.getLayoutInflater().inflate(R.layout.garden_map_water_3out, null);
            tv_switch1 = (TextView) waterView.findViewById(R.id.tv_switch1);
            tv_switch2 = (TextView) waterView.findViewById(R.id.tv_switch2);
            img_water = (ImageView) waterView.findViewById(R.id.img_water);

            if (water.getWaterState() == 1)
                img_water.setImageResource(R.mipmap.garden_ic_water3_online);
            else
                img_water.setImageResource(R.mipmap.garden_ic_water3_offline);

        }

        tv_switch0 = (TextView) waterView.findViewById(R.id.tv_switch0);
        tv_waterName = (TextView) waterView.findViewById(R.id.tv_waterName);
        tv_waterName.setText(water.getWaterName());

        int[] switchsTime = water.getSwitchsTime();

        if (switchsTime != null) {
            if (switchsTime.length >= 1) {
                if (switchsTime[0] > 0)
                    tv_switch0.setBackgroundResource(R.color.garden_switch_on);
                else
                    tv_switch0.setBackgroundResource(R.color.garden_switch_off);
            }
            if (switchsTime.length >= 2 && tv_switch1 != null) {

                if (switchsTime[1] > 0)
                    tv_switch1.setBackgroundResource(R.color.garden_switch_on);
                else
                    tv_switch1.setBackgroundResource(R.color.garden_switch_off);
            }
            if (switchsTime.length >= 3 && tv_switch2 != null) {
                if (switchsTime[2] > 0)
                    tv_switch2.setBackgroundResource(R.color.garden_switch_on);
                else
                    tv_switch2.setBackgroundResource(R.color.garden_switch_off);
            }
        }


        img_water.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                tv_waterName.setText(water.getWaterName());
                if (tv_waterName.getVisibility() == View.VISIBLE)
                {
                    tv_waterName.setVisibility(View.INVISIBLE);
                }
                else
                {
                    tv_waterName.setVisibility(View.VISIBLE);
                }
            }
        });

//        img_water.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                tv_waterName.setText(water.getWaterName());
//                if (tv_waterName.getVisibility() == View.VISIBLE)
//                {
//                    tv_waterName.setVisibility(View.INVISIBLE);
//                }
//                else
//                {
//                    tv_waterName.setVisibility(View.VISIBLE);
//                }
//
//                return true;
//            }
//        });

        tv_waterName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(activity, Garden_Water_Activity.class);
                intent.putExtra("water", water);
                activity.startActivity(intent);

            }
        });

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(System_Dimen_Util.dip2px(activity, 12), System_Dimen_Util.dip2px(activity, 8));
        tv_switch0.setLayoutParams(params);
        tv_switch0.setText("1");
        tv_switch0.setTextSize(System_Dimen_Util.sp2px(activity, 2));

        if (tv_switch1 != null) {
            tv_switch1.setLayoutParams(params);
            tv_switch1.setText("2");
            tv_switch1.setTextSize(System_Dimen_Util.sp2px(activity, 2));
        }

        if (tv_switch2 != null) {
            tv_switch2.setLayoutParams(params);
            tv_switch2.setText("3");
            tv_switch2.setTextSize(System_Dimen_Util.sp2px(activity, 2));
        }
        img_water.setClickable(false);

        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(System_Dimen_Util.dip2px(activity, 40), System_Dimen_Util.dip2px(activity, 30));
        img_water.setLayoutParams(params1);

//        LinearLayout.LayoutParams params2=new LinearLayout.LayoutParams(System_Dimen_Util.dip2px(activity,50), ViewGroup.LayoutParams.WRAP_CONTENT);
//        tv_waterName.setLayoutParams(params2);
    }
    public void showHideDeviceName(boolean b)
    {
        if (b)
        {
            img_water.setClickable(true);
            tv_waterName.setTextColor(Color.BLACK);
            tv_waterName.setClickable(true);
        }
        else
        {
            img_water.setClickable(false);
            tv_waterName.setClickable(false);
        }

    }
    public void allSwitchsOpened() {

        if (water.getWaterState()==1) {

            tv_switch0.setBackgroundResource(R.color.garden_switch_on);
            if (tv_switch1!=null)
                tv_switch1.setBackgroundResource(R.color.garden_switch_on);

            if (tv_switch2!=null)
                tv_switch2.setBackgroundResource(R.color.garden_switch_on);

            if (deviceType == 1)
                img_water.setImageResource(R.mipmap.garden_ic_water1_online);
            else if (deviceType == 2)
                img_water.setImageResource(R.mipmap.garden_ic_water2_online);
            else
                img_water.setImageResource(R.mipmap.garden_ic_water3_online);
        }

        tv_waterName.setVisibility(View.GONE);
    }

    public void allSwitchsClosed()
    {
        if (water.getWaterState()==1)
        {
            tv_switch0.setBackgroundResource(R.color.garden_switch_off);
            if (tv_switch1!=null)
                tv_switch1.setBackgroundResource(R.color.garden_switch_off);

            if (tv_switch2!=null)
                tv_switch2.setBackgroundResource(R.color.garden_switch_off);

            if (deviceType == 1)
                img_water.setImageResource(R.mipmap.garden_ic_water1_online);
            else if (deviceType == 2)
                img_water.setImageResource(R.mipmap.garden_ic_water2_online);
            else
                img_water.setImageResource(R.mipmap.garden_ic_water3_online);
        }
        tv_waterName.setVisibility(View.GONE);
    }

    public void openSwitchsFailure(String waterIds,int waterLength)
    {
        if (water!=null)
        {
            if (waterIds.contains(water.getWaterId()))
            {
                tv_switch0.setBackgroundResource(R.color.garden_switch_off);
                if (tv_switch1!=null)
                    tv_switch1.setBackgroundResource(R.color.garden_switch_off);
                if (tv_switch2!=null)
                    tv_switch2.setBackgroundResource(R.color.garden_switch_off);

                tv_waterName.setTextColor(Color.RED);
                tv_waterName.setVisibility(View.VISIBLE);

                if (waterLength==0)
                    tv_waterName.setText("关闭失败");
                else
                    tv_waterName.setText("打开失败");
            }
            else
            {
                if (waterLength>0)
                {
                    tv_switch0.setBackgroundResource(R.color.garden_switch_on);
                    if (tv_switch1!=null)
                        tv_switch1.setBackgroundResource(R.color.garden_switch_on);
                    if (tv_switch2!=null)
                        tv_switch2.setBackgroundResource(R.color.garden_switch_on);
                }
                else
                {
                    tv_switch0.setBackgroundResource(R.color.garden_switch_off);
                    if (tv_switch1!=null)
                        tv_switch1.setBackgroundResource(R.color.garden_switch_off);
                    if (tv_switch2!=null)
                        tv_switch2.setBackgroundResource(R.color.garden_switch_off);
                }

            }


        }
    }

    public void setLeftScale(float leftScale) {
        this.leftScale = leftScale;
    }

    public float getLeftScale() {
        return this.leftScale;
    }

    public void setTopScale(float topScale) {
        this.topScale = topScale;
    }

    public float getTopScale() {
        return this.topScale;
    }

    public void setWaterView(View waterView) {
        this.waterView = waterView;
    }

    public View getWaterView() {
        return this.waterView;
    }


}
